<template>
  <div class="role-management">
    <el-card>
      <div slot="header" class="clearfix">
        <span>角色管理</span>
        <!-- 使用权限指令控制按钮显示 -->
        <el-button 
          type="primary" 
          size="small" 
          class="btn-add"
          @click="handle_add"
        >
          <i class="el-icon-plus"></i> 新增角色
        </el-button>
      </div>
      
      <!-- 搜索栏 -->
      <div class="search-form">
        <el-form :inline="true" :model="query_params">
          <el-form-item label="角色名称">
            <el-input v-model.trim="query_params.role_name" placeholder="请输入角色名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="角色状态">
            <el-select v-model="query_params.status" placeholder="请选择状态" clearable>
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handle_query">
              <i class="el-icon-search"></i> 查询
            </el-button>
            <el-button @click="handle_reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 数据表格 -->
      <el-table 
        v-loading="loading" 
        :data="role_list" 
        border
        style="width: 100%"
      >
        <el-table-column prop="role_id" label="角色ID"></el-table-column>
				<el-table-column prop="role_name" label="角色名称"></el-table-column>
				<el-table-column prop="role_key" label="角色标识"></el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch 
              v-model="scope.row.status" 
              active-value="1" 
              inactive-value="0"
              @change="handle_status_change(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              size="small"
              @click="handle_edit(scope.row)"
            >
              编辑
            </el-button>
            
            <el-button 
              type="warning" 
              size="small"
              @click="handle_assign_menu(scope.row)"
            >
              分配菜单
            </el-button>
            
            <el-button 
              type="danger" 
              size="small"
              @click="handle_delete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-size.sync="page_size"
          :current-page.sync="current_page"
          @size-change="handle_size_change"
          @current-change="handle_current_change"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import roleApi from '@/api/sys-role'

export default {
  name: 'RoleManagement',
  data() {
    return {
      loading: false,
      role_list: [],
      total: 0,
      current_page: 1,
      page_size: 10,
      query_params: {
        role_name: '',
        status: ''
      }
    }
  },
  created() {
    this.get_role_list()
  },
  methods: {
    // 获取角色列表
    get_role_list() {
				this.loading = true
				roleApi.list(this.query_params).then(res => {
        this.role_list = res.data.list
        this.total = res.data.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    
    // 查询
    handle_query() {
      this.current_page = 1
      this.get_role_list()
    },
    
    // 重置
    handle_reset() {
      this.query_params = {
        role_name: '',
        status: ''
      }
      this.handle_query()
    },
    
    // 新增角色
    handle_add() {
      this.$router.push('/system/role/add')
    },
    
    // 编辑角色
    handle_edit(row) {
      this.$router.push({
        path: '/system/role/edit',
        query: { role_id: row.role_id }
      })
    },
    
    // 分配菜单
    handle_assign_menu(row) {
      this.$router.push({
        path: '/system/role/assignMenu',
        query: { role_id: row.role_id }
      })
    },
    
    // 删除角色
    handle_delete(row) {
      this.$confirm(`确定要删除角色"${row.role_name}"吗？`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        roleApi.delete(row.role_id).then(() => {
          this.$message.success('删除成功')
          this.get_role_list()
        })
      })
    },
    
    // 切换角色状态
    handle_status_change(row) {
      // 使用update方法更新角色状态
      roleApi.update({ id: row.role_id, status: row.status }).then(() => {
        this.$message.success('状态更新成功')
      }).catch(() => {
        // 如果失败，回滚状态
        row.status = row.status === '1' ? '0' : '1'
      })
    },
    
    // 分页大小变化
    handle_size_change(val) {
      this.page_size = val
      this.get_role_list()
    },
    
    // 当前页变化
    handle_current_change(val) {
      this.current_page = val
      this.get_role_list()
      }
    }
  }

</script>

<style scoped>
.role-management {
  padding: 20px;
}

.btn-add {
  float: right;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>